package com.zsz;

import com.zsz.domain.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resources;
import java.io.InputStream;
import java.util.List;

public class MyApp {
    public static void main(String[] args)  {
        //访问mybatis,读取student数据
        //1.定义mybatis主配置文件的名称，从类路径的根开始（target.classes）
        String config="/mybatis.xml";
        //2.读取这个config表示的文件
        InputStream inputStream=Resources.class.getResourceAsStream(config);
        //3.创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //4.创建SqlSessionFactory对象
        SqlSessionFactory factory = sqlSessionFactoryBuilder.build(inputStream);
        //5.[重要]获取SqlSession对象，从SqlSessionFactory中获取SqlSession
        SqlSession sqlSession=factory.openSession();
        //6.[重要]指定要执行的sql语句的标识。sql映射文件的namespace+"."+标签的id值。
        String sqlId="com.zsz.dao.StudentDao"+"."+"selectStudents";
//        String sqlId="a"+"."+"b";
        //7.执行sql语句，通过sqlId找到语句
        List<Student> studentList = sqlSession.selectList(sqlId);
        //8.输出结果
//        studentList.forEach(stu-> System.out.println(stu));
        for (Student stu:studentList) {
            System.out.println("查询的学生="+stu);

        }

        //9.关闭SqlSession对象
        sqlSession.close();


    }

}
